package org.cliff.message.platform.core.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.cliff.message.platform.core.domain.InterfaceConfig;
import org.cliff.message.platform.core.mapper.InterfaceConfigMapper;
import org.cliff.message.platform.core.util.UriUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口配置缓存维护者，避免每次请求处理都查数据库，接口配置定时刷新
 *
 * @author 刘文
 * @date 2023/10/16
 */
@Component
public class InterfaceConfigMaintainer implements ApplicationListener<ContextRefreshedEvent> {

// @Autowired
    // private InterfaceConfigService interfaceConfigService;

    @Autowired
    private InterfaceConfigMapper interfaceConfigMapper;

    /**
     * 请求URI到接口配置 map
     */
    private static Map<String, InterfaceConfig> requestUriToInterfaceConfigMap = new HashMap<>();

    /**
     * FunctionCode到接口配置 map
     */
    private static Map<String, InterfaceConfig> functionCodeToInterfaceConfigMap = new HashMap<>();

    /**
     * 缓存维护
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void maintainCache() {
        refreshInterfaceConfigCache();
    }

    private void refreshInterfaceConfigCache() {
        LambdaQueryWrapper<InterfaceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InterfaceConfig::getEnumDelete, "NORMAL");
        List<InterfaceConfig> interfaceConfigs = interfaceConfigMapper.selectList(queryWrapper);
        Map<String, InterfaceConfig> requestUriToInterfaceConfigMap = new HashMap<>();
        Map<String, InterfaceConfig> functionCodeToInterfaceConfigMap = new HashMap<>();
        for (InterfaceConfig interfaceConfig : interfaceConfigs) {
            requestUriToInterfaceConfigMap.put(UriUtils.getUriPathFromUrl(interfaceConfig.getUrl()), interfaceConfig);
            functionCodeToInterfaceConfigMap.put(interfaceConfig.getFunctionCode(), interfaceConfig);
        }
        this.requestUriToInterfaceConfigMap = requestUriToInterfaceConfigMap;
        this.functionCodeToInterfaceConfigMap = functionCodeToInterfaceConfigMap;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        refreshInterfaceConfigCache();
    }

    /**
     * 根据请求URI获取对应的接口配置
     * @param requestUri
     * @return
     */
    public static InterfaceConfig getInterfaceConfigByRequestUri(String requestUri) {
        return requestUriToInterfaceConfigMap.get(requestUri);
    }

    /**
     * 根据请求function code获取对应的接口配置
     * @param functionCode
     * @return
     */
    public static InterfaceConfig getInterfaceConfigByFunctionCode(String functionCode) {
        return functionCodeToInterfaceConfigMap.get(functionCode);
    }
}
